package com.qf.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.qf.entity.User;
import com.qf.service.IUserService;
import com.qf.utils.MapCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/user")
@RestController
@CrossOrigin//解决跨域问题
public class UserController {
    //注入
    @Autowired
    private IUserService userService;
    @Autowired
    private DefaultKaptcha kaptcha;
    //用来暂存验证码的map
    private Map<String, String> codeMap = new HashMap<>();

    //注册部分
    @RequestMapping(value = "/register")
    public Integer register(User user) {
        int result = userService.register(user);
        System.out.println("注册信息" + user);
        return result;
    }

    /*登录部分*/
    @RequestMapping(value = "/login")

    public User login(String username, String password, String code, String codekey) {
        System.out.println("接收到登录请求：" + username + "--" + password + "--" + code);
        //获取服务器生成的验证码

        String serverCode = codeMap.get(codekey);
        if (!code.equals(serverCode)) {
            User user = new User();
            user.setUsername("-1");
            return user;
        }
        //验证码匹配正常,删除map中保存的验证码
        codeMap.remove(codekey);
        //user为null说明错了，
        User user = userService.login(username, password);
        return user;

    }


    /*生成验证码*/
    @RequestMapping(value = "/code")
    public void creatCode(String codekey, HttpSession session, HttpServletResponse response) throws IOException {
        //生成验证码
        String code = kaptcha.createText();
        //保存到Map集合中
        codeMap.put(codekey, code);
        //生成图片
        BufferedImage image = kaptcha.createImage(code);
        //直接将图片通过response对象响应给客户端
        ImageIO.write(image, "jpg", response.getOutputStream());

    }

    /*找回密码*/
    @RequestMapping("/sendEmailCode")
    public int sendEmailCode(String username) {
        int result = userService.sendEmailByUserName(username);
        return result;
    }

    //
    @RequestMapping("/updatePassword")

    public int updatePassword(String username, String newpassword, String code) {
        //校验验证码，用户输入的验证码-发邮件的验证码（一次性，时效性）
        //获取原来发邮件的验证码
        String oldCode = MapCodeUtils.getCode(username);
        if (oldCode == null) {
            return -1;//无效/已过期的验证码
        }
        //比较验证码
        if (!oldCode.equals(code)) {
            //验证码有误
            return -2;
        }
        //验证码已通过
        //调用service
        int result = userService.updatePassword(username, newpassword);
        //将验证码删除
        MapCodeUtils.removeCode(username);
        return result;
    }
}
